<?php

namespace common\models\c2\entity;

use backend\models\c2\entity\rbac\BeUser;
use Yii;
use yii\behaviors\BlameableBehavior;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%production_commit_note}}".
 *
 * @property string $id
 * @property integer $type
 * @property string $schedule_id
 * @property string $label
 * @property string $memo
 * @property string $commit_at
 * @property string $updated_by
 * @property string $created_by
 * @property integer $state
 * @property integer $status
 * @property integer $position
 * @property string $updated_at
 * @property string $created_at
 */
class ProductionCommitNote extends \cza\base\models\ActiveRecord
{

    public $items;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%production_commit_note}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['memo'], 'string'],
            [['commit_at', 'updated_at', 'created_at', 'items'], 'safe'],
            [['updated_by', 'created_by', 'position'], 'integer'],
            [['type', 'state', 'status'], 'integer', 'max' => 4],
            [['schedule_id'], 'string', 'max' => 10],
            [['label'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'type' => Yii::t('app.c2', 'Type'),
            'schedule_id' => Yii::t('app.c2', 'Schedule ID'),
            'label' => Yii::t('app.c2', 'Label'),
            'memo' => Yii::t('app.c2', 'Memo'),
            'commit_at' => Yii::t('app.c2', 'Commit At'),
            'updated_by' => Yii::t('app.c2', 'Updated By'),
            'created_by' => Yii::t('app.c2', 'Created By'),
            'state' => Yii::t('app.c2', 'State'),
            'status' => Yii::t('app.c2', 'Status'),
            'position' => Yii::t('app.c2', 'Position'),
            'updated_at' => Yii::t('app.c2', 'Updated At'),
            'created_at' => Yii::t('app.c2', 'Created At'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\ProductionCommitNoteQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\ProductionCommitNoteQuery(get_called_class());
    }
    
    /**
    * setup default values
    **/
    public function loadDefaultValues($skipIfSet = true) {
        parent::loadDefaultValues($skipIfSet);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            BlameableBehavior::className()
        ]);
    }

    public function getCreator()
    {
        return $this->hasOne(BeUser::className(), ['id' => 'created_by']);
    }


    public function getNoteItems()
    {
        return $this->hasMany(ProductionCommitNoteItem::className(), ['note_id' => 'id']);
    }

    public function loadItems()
    {
        $this->items = $this->getNoteItems()->all();
    }

    public function beforeDelete()
    {
        foreach ($this->noteItems as $noteItem) {
            $noteItem->delete();
        }
        return parent::beforeDelete(); // TODO: Change the autogenerated stub
    }

}
